package com.bocloud.cmp.service.config;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bocloud.cmp.dao.DictionaryDao;
import com.bocloud.cmp.entity.Dictionary;
import com.bocloud.cmp.interfaces.config.DictService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.utils.Common;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 字典服务实现类
 * 
 * @author dmw
 *
 */
@Service("dictService")
public class DictServiceImpl implements DictService {

	private static Logger logger = LoggerFactory.getLogger(DictServiceImpl.class);
	@Autowired
	private DictionaryDao dictionaryDao;

	@Override
	public BsmResult list(String value) {
		try {
			List<Map<String, Object>> data = Lists.newArrayList();
			List<Dictionary> dicts = dictionaryDao.list(value);
			for (Dictionary dict : dicts) {
				Map<String, Object> map = Maps.newHashMap();
				map.put(Common.ID, dict.getId());
				map.put(Common.NAME, dict.getName());
				map.put(Common.VALUE, dict.getValue());
				map.put("pvalue", dict.getPvalue());
				List<Dictionary> children = dictionaryDao.list(dict.getValue());
				if (null != children && !children.isEmpty()) {
					map.put("children", "[]");
				}
				data.add(map);
			}
			return new BsmResult(true, data, "查询字典成功");
		} catch (Exception e) {
			logger.error("list dictionary fail:", e);
			return new BsmResult(false, "查询字典失败", "", "");
		}
	}

	@Override
	public BsmResult listChild(String pid) {
		try {
			List<Object> dicts = this.dictionaryDao.listChildren(pid);
			return new BsmResult(true, dicts, "success");
		} catch (Exception e) {
			logger.error("List Children Dictionary exception:", e);
			return new BsmResult(false, "获取字典信息失败", "505", "");
		}
	}

}